#!/usr/bin/env python
#-*- coding: utf-8 -*-
#__author__:vincentlc
#time: 16/4/3 : 15:54

'''
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
'''

class Solution(object):
    def romanToInt(self, s):
        """
        :type s: str
        :rtype: int
        """
        #
        dict_a = {"I":1,"V":5,"X":10,"L":50,"C":100,"D":500,"M":1000}
        dict_b = {"IV":4,"IX":9,"XL":40,"XC":90,"CD":400,"CM":900}
        sum = 0
        privious = s[0]
        for i in s:
            tmp = privious+i
            if tmp in dict_b:
                sum = sum+dict_b[tmp]-dict_a[privious]
                privious = ''
            else:
                sum += dict_a[i]
                privious = i
        return sum


a = Solution()
test_a = 'DCXXI'            #621
test_b = 'MCMXCVI'          #1996
test_c = 'MCMXC'            #1954
test_d = 'MCMLIV'           #1990
test_e = 'MMXIV'            #2014

print (a.romanToInt(test_a))
print (a.romanToInt(test_b))
print (a.romanToInt(test_c))
print (a.romanToInt(test_d))
print (a.romanToInt(test_e))

